package org.zzh.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * @author 十里坡
 * @description 配置SpringMVC的纯注解进行IOC容器配置，取消了Spring的IOC的容器，避免了父子容器的创建
 */
public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // 返回根上下文的配置类
        // 替换为你的根上下文配置类
        return new Class<?>[]{};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // 返回 Spring MVC 的配置类
        // 替换为你的 Spring MVC 配置类
        return new Class<?>[]{SpringMVCConfig.class};
    }


    @Override
    protected String[] getServletMappings() {
        // 返回 DispatcherServlet 的映射路径
        return new String[]{"/"};
    }

    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        DelegatingFilterProxy shiroFilterProxy = new DelegatingFilterProxy();
        // 指定目标 Bean 的名称
        shiroFilterProxy.setTargetBeanName("shiroFilter");
        // 确保生命周期与 Spring 管理的 Bean 一致
        shiroFilterProxy.setTargetFilterLifecycle(true);

        return new Filter[]{characterEncodingFilter, shiroFilterProxy};
    }
}